package com.chen.springboot.controller.uniapp;

import com.chen.springboot.dto.uniapp.PageResult;
import com.chen.springboot.common.Result;
import com.chen.springboot.service.Impl.uniapp.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = {"商品接口"})
@RequestMapping("/api")
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/goods/list")
    @ApiOperation("分页查询商品列表")
    public Result<PageResult> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(defaultValue = "") String keyword) {

        // 模拟慢速响应，延迟 3 秒
        try {
            Thread.sleep(2000); // 3000 毫秒 = 3秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 调用服务层获取分页数据
        PageResult pageResult = goodsService.findPages(page, pageSize, keyword);

        // 如果没有数据，返回"没有更多数据了"的提示
        if (pageResult.getItems().isEmpty()) {
            return Result.failure("没有更多数据了");
        }

        // 返回封装好的分页数据
        return Result.success(pageResult);
    }
}
